package allove.eric.oeditor;
/* by Allove Eric Sun @ 20091212
 *  contact me with eric.sunjian@gmail.com
 * this is the main Activity , 
 * users will modify or save text here !
 * */
import java.io.File;

import allove.eric.oeditor.common.FileLoader;
import allove.eric.oeditor.common.FinalUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View ;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class HiEditor extends Activity {

	private final String TAG = "HiEditor" ;
    private final boolean debug = FinalUtils.isDebug ;
    public static final String KEY_FILE_PATH = "file_path" ;   
    public static final String KEY_FOR_WHAT = "for_what" ;

    
    private EditText contentContainer ;
    private Handler handler ;
    private StringBuffer content ;
    
    private final int REQ_FOR_FILE_OPEN = 11 ;
    private final int REQ_FOR_FILE_SAVE = 15 ;
    private final int STATUS_LOAD_FILE_DONE = 12 ;
    public static final int FOR_OPEN = 13 ;
    public static final int FOR_SAVE = 14 ;
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS) ;
        setContentView(R.layout.main);
        this.initView() ;
        this.buildeHandler() ;
    }
    
    private void initView(){
    	this.contentContainer = (EditText)this.findViewById(R.id.content) ;
    }
    
    private void buildeHandler(){
    	handler = new Handler(){
    		public void handleMessage(Message msg){
    			switch(msg.what){
    			case STATUS_LOAD_FILE_DONE : {
    				contentContainer.setText(content.toString()) ;
    				setProgressBarIndeterminateVisibility(false) ;
    			} ; break ;
    			}
    		}
    	} ;
    }

    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		menu.add(0, 0, 0, "打开") ;
		menu.add(0, 1, 1, "清空") ;
		menu.add(0, 2, 2, "保存");
		return true ;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);
		switch(item.getItemId()){
		case 0 : {
			Bundle b = new Bundle();
			b.putInt(KEY_FOR_WHAT, FOR_OPEN) ;
			Intent i = new Intent() ;
			i.setClass(this, FileChooser.class) ;
			i.putExtras(b) ;
			startActivityForResult(i , REQ_FOR_FILE_OPEN ) ;
		} ; break ;
		
		case 1 : {
			this.contentContainer.setText("") ;
		} ; break ;
		
		case 2 : {
			Bundle b = new Bundle();
			b.putInt(KEY_FOR_WHAT, FOR_SAVE) ;
			Intent i = new Intent() ;
			i.setClass(this, FileChooser.class) ;
			i.putExtras(b) ;
			startActivityForResult(i , REQ_FOR_FILE_SAVE ) ;
		} ;  break ;
		}
		return true ;
	}
	
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK){
			switch(requestCode){
			case REQ_FOR_FILE_OPEN : {
				String path = data.getExtras().getString(KEY_FILE_PATH) ;
				this.setProgressBarIndeterminateVisibility(true) ;
				this.laodFile(path) ;
			} ; break ;
			
			case REQ_FOR_FILE_SAVE : {
				
			} ; break ;
			
			}
		}
	}
    
    private void laodFile(final String path){
    	new Thread(){
    		public void run() {
    			content = FileLoader.getInstance().getTextContent(path) ;
    			Message msg = new Message() ;
    			msg.what = STATUS_LOAD_FILE_DONE ;
    			handler.sendMessage(msg) ;
    		}
    	}.start() ;
    }
}